package oa.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import oa.biz.DocumentBiz;
import oa.entity.Document;

@Controller
@RequestMapping("/admin")
public class DocumentController {
	@Autowired
	private DocumentBiz docBiz;
	
	@RequestMapping("/getDocuments")
	@ResponseBody
	public List<Document> getList(){
		return docBiz.getAll();
	}
	@RequestMapping("/Document")
	public String document(){
		return "Document";
	}
	@RequestMapping("/getChild")
	@ResponseBody
	public List<Document> getChild(int parentId){
		return docBiz.getChild(parentId);		
	}
	
	@RequestMapping("/fetchDocument")
	@ResponseBody
	public Document fetchDocumentById(int id){
		return docBiz.fetchDocumentById(id);
	}
	
	@Transactional
	@RequestMapping(value="/documentSave",method=RequestMethod.POST)
	public String save(Document doc,@RequestParam(name="id",required=false,defaultValue="0")int id){
		if(id==0){
			docBiz.addDocument(doc);
			return "redirect:Document";	
		}else{
			docBiz.updateDocument(doc);
			return "redirect:Document";
		}			
	}
	
	@RequestMapping("/editDocument")
	@ResponseBody
	public Document edit(int id){
		return docBiz.fetchDocumentById(id);
		
		
	}
}
